package com.play.ballen.me.ui

import android.content.Intent
import android.view.View
import com.hjq.toast.ToastUtils
import com.play.ballen.R
import com.play.ballen.config.KVConst
import com.play.ballen.databinding.FragmentMeBinding
import com.play.ballen.home.ui.PlayCountRecordActivity
import com.play.ballen.home.ui.ServiceActivity
import com.play.ballen.me.vm.UserInfoVM
import com.play.ballen.splash.SplashActivity
import com.play.ballen.utils.UserInfoManager
import com.yuven.appframework.manager.AppActivitysManager
import com.yuven.appframework.util.ClipboardUtils
import com.yuven.appframework.util.imageloader.ImageUtil
import com.yuven.appframework.util.setOnSingleClickListener
import com.yuven.baselib.component.BaseMVVMFragment
import com.yuven.baselib.utils.ResourceUtil
import com.yuven.baselib.utils.ViewUtils

class MeFragment : BaseMVVMFragment<UserInfoVM, FragmentMeBinding>() {

    companion object {
        val TAG: String = MeFragment::class.java.simpleName

        fun newInstance(): MeFragment {
            return MeFragment()
        }
    }

    override fun initIntent() {}

    override fun layoutId(): Int {
        return R.layout.fragment_me
    }

    override fun viewModelClass(): Class<UserInfoVM> {
        return UserInfoVM::class.java
    }

    private val onClickListener = tag@{ v: View ->
        when (v.id) {
//            R.id.imgSetting -> {
//                SettingActivity.start(mContext)
//            }

            R.id.tvInviteCode -> {
                if (viewModel.memberIndexInfo.value == null) {
                    return@tag
                }
                ClipboardUtils.setText(viewModel.memberIndexInfo.value!!.invite_code, mContext)
                ToastUtils.show(ResourceUtil.getString(R.string.me_copy))
            }

            R.id.btnRecharge -> {
                ToastUtils.show(ResourceUtil.getString(R.string.weihu))
//                RechargeActivity.start(mContext)
            }

            R.id.btnExtra -> {
                WithDrawActivity.start(mContext)
            }

            R.id.btnTransfer -> {
                TransferActivity.start(mContext)
            }

            R.id.btnDetail -> {
                MoneyLogActivity.start(
                    mContext,
                    ResourceUtil.getString(R.string.detail_title),
                    "",
                    KVConst.MONEY_LOG_MONEY_TYPE_ALL
                )
            }

            R.id.btnToBeClaimed -> {
                ClaimedActivity.start(mContext)
            }

            R.id.btnReceiveMainValue -> {
                ReceiveValueActivity.start(mContext, ReceiveValueActivity.TYPE_MAIN_VALUE)
            }

            R.id.btnReceiveSportValue -> {
                ReceiveValueActivity.start(mContext, ReceiveValueActivity.TYPE_SPORT_VALUE)
            }

            R.id.llTeamCenter -> {
                TeamCenterActivity.start(mContext)
            }

            R.id.llPoster -> {
                InvitePosterActivity.start(mContext)
            }

            R.id.llContacts -> {
                ServiceActivity.start(mContext)
            }

            R.id.llSlapRecord -> {
                PlayCountRecordActivity.start(mContext)
            }
            R.id.imgSetting->{
                viewModel.tokenStatus.value=-1
                viewModel.imLoginStatus.value=-1
                UserInfoManager.setToken("")
                UserInfoManager.logout()
                AppActivitysManager.getInstance().finishAllActivity()
                val intent = Intent(mContext, SplashActivity::class.java)
                intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
                startActivity(intent)
            }
        }
    }

    override fun initView() {
        dataBinding.fakeStatusBar.layoutParams.height = ViewUtils.getStatusHeight()

        dataBinding.imgSetting.setOnSingleClickListener(onClickListener)
        dataBinding.tvInviteCode.setOnSingleClickListener(onClickListener)
        dataBinding.btnRecharge.setOnSingleClickListener(onClickListener)
        dataBinding.btnExtra.setOnSingleClickListener(onClickListener)
        dataBinding.btnTransfer.setOnSingleClickListener(onClickListener)
        dataBinding.btnDetail.setOnSingleClickListener(onClickListener)
        dataBinding.btnToBeClaimed.setOnSingleClickListener(onClickListener)
        dataBinding.btnReceiveMainValue.setOnSingleClickListener(onClickListener)
        dataBinding.btnReceiveSportValue.setOnSingleClickListener(onClickListener)
        dataBinding.llTeamCenter.setOnSingleClickListener(onClickListener)
        dataBinding.llPoster.setOnSingleClickListener(onClickListener)
        dataBinding.llContacts.setOnSingleClickListener(onClickListener)
        dataBinding.llSlapRecord.setOnSingleClickListener(onClickListener)
    }

    override fun initData() {
//        viewModel.getMemberIndex()
    }

    override fun initObserver() {
        super.initObserver()
        viewModel.memberIndexInfo.observe(this) {
            it.run {
                ImageUtil.instance.loadCircleImage(avatar, dataBinding.imgAvatar, 0, 0)
                if (!nick_name.isNullOrEmpty()) {
                    dataBinding.tvName.visibility = View.VISIBLE
                    dataBinding.tvName.text = nick_name
                } else {
                    dataBinding.tvName.visibility = View.GONE
                }
                dataBinding.tvUID.text = "UID: $id"
                dataBinding.tvInviteCode.text =
                    ResourceUtil.getString(R.string.register_code_invite) + ": $invite_code"
                dataBinding.tvMoney.text = money
                dataBinding.tvRate.text = ResourceUtil.getString(R.string.me_rate, rate)
                dataBinding.tvTodayDestroy.text = today_destroy
                dataBinding.tvTotalDestroy.text = total_destroy
                dataBinding.tvMainValue.text = money1
                dataBinding.tvSportValue.text = money2
                dataBinding.tvToBeClaimedValue.text = money3
            }
        }
    }

    override fun onResume() {
        super.onResume()
//        viewModel.getMemberIndex()
    }

}